#include "NPC.h"

NPC::NPC(void){
	idleTime = false;
	saveDir = false;
	this->SetSpeed(100.0f);

	moveStart = 0;
	moveEnd = 0;

	rndNum = rand() % 100 + 1;
	
	if(rndNum < 50)
		this->SetDirection(Vector3D(1.0, 0.0, 0.0));
	else
		this->SetDirection(Vector3D(-1.0, 0.0, 0.0));
}

NPC::~NPC(void){
}

void NPC::Idle(void){
	if(this->GetOnGround()){
		if(!idleTime){
			this->SetSpriteImageNumber(0);

			rndNum = rand() % 100 + 1;
			if(rndNum < 25){
				this->SetDirection(Vector3D());
			}else{
				rndNum = rand() % 100 + 1;

				if(rndNum < 50)
					this->SetDirection(Vector3D(1.0, this->GetDirection().y, 0.0));
				else
					this->SetDirection(Vector3D(-1.0, this->GetDirection().y, 0.0));
			}

			countdownTimer.StartTimer(0, 0, 5, 0);
			idleTime = true;
		}

		if(idleTime && countdownTimer.CheckIfTimerEnd()){
			//countdownTimer.StartTimer(0, 0, 5, 0);
			idleTime = false;
		}
	}
}

void NPC::WallHit(float elapsed){
	if(!saveDir){
		this->oldDir = this->GetDirection();
		saveDir = true;
	}

	if(this->GetOnGround()){
		this->SetDirection(Vector3D(oldDir.x * -1, oldDir.y, 0.0));
		Object::WallHit(elapsed);
		saveDir = false;
	}else{
		this->SetDirection(Vector3D());
	}
}

void NPC::InitMoveAnimation(int start, int end){
	this->moveStart = start;
	this->moveEnd = end;
}

bool NPC::UpdateObject(Vector3D offset, float elapsed){
	Object::UpdateObject(offset, elapsed);

	Idle();

	if(this->GetDirection().x == 1){
		this->invertImage = true;
		//this->MoveMeLeftRight(true, elapsed);
		this->PlaySpriteAnimation(moveStart, moveEnd, 500);
	}else if(this->GetDirection().x == -1){
		this->invertImage = false;
		//this->MoveMeLeftRight(false, elapsed);
		this->PlaySpriteAnimation(moveStart, moveEnd, 500);
	}

	this->SetPosition(this->GetPosition() + this->GetDirection() * this->GetSpeed() * elapsed);

	if(countdownTimer.CheckIfTimerRunning())
		countdownTimer.UpdateTimer();

	return false;
}